const path = require('path')
const glob = require("glob")
const CompressionWebpackPlugin = require('compression-webpack-plugin')
const URL_TIMER = process.env.BUILD_TIMER ? '?time='+process.env.BUILD_TIMER : ''
// var express = require('express')
// var axios = require('axios')
// var app = express()
// var apiRoutes = express.Router()
function resolve (dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  assetsDir: 'v2-h5-static',
	chainWebpack: config => {
    config.entry.app = ['./src/main.js']
		config
		  .plugin('html')
		  .tap(args => {
			args[0].base_url = process.env.VUE_APP_BASE_URL
			return args
		})
		config.module
			.rule('media')
			.test(/\.(mp4|webm|ogg|mp3|wav|flac|aac|swf)(\?.*)?$/)
			.use('url-loader')
			.loader('url-loader')
			.tap(options => {
				return options
			})
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
		config
			  .when(process.env.NODE_ENV !== 'development',config=>{
					config.optimization.splitChunks({
					  chunks: 'all',
					  cacheGroups: {
							libs: {
								name: "chunk-libs",
								test: /[\\/]node_modules[\\/]/,
								priority: 10,
								chunks: "initial" // 只打包初始时依赖的第三方
							}
					  }
					})
				})
  },
	productionSourceMap: false,
	transpileDependencies:[/vue2-layer-mobile/,/flv.js/,/vue-skeleton-loading/],
	publicPath:process.env.NODE_ENV === 'production' ? 'http://static30.uvod.cn/':'/',
	devServer: { // 配置服务器
		host: '0.0.0.0',
		port: 80,
		open: true,
		https: false,
		overlay: {
			warnings: false,
			errors: true
		},
		disableHostCheck: true,
		// //添加一个before方法
		// before(apiRoutes){
		// 	apiRoutes.get('/api/v1/pay/weixin.html',(req,res)=>{
		// 		console.log('请求地址:'+process.env.VUE_APP_API)
		// 		axios({
		// 			method: 'post',
		// 			url: process.env.VUE_APP_API + 'pay/weixin.html',
		// 			data: req.query,
		// 			headers: {
		// 				referer: 'http://www.9ox.cn/',  //源域名
		// 				host: '9ox.cn',
		// 				Authorization: req.query.token,
		// 				'Pay-Domain': req.query.domain
		// 			}
		// 		}).then((response) => {
  //         //response是api地址返回的，数据在data里。
  //         res.json(response.data)
  //       }).catch((e) => {
		// 			res.json(e)
  //       })
		// 	});
		// 	app.use('/api', apiRoutes)
		// }
	},
	configureWebpack: { // 覆盖webpack默认配置的都在这里
		output: { // 输出重构
	  	filename: 'v2-h5-static/js/'+`[name].[hash].js`+URL_TIMER,
	  	chunkFilename:'v2-h5-static/js/'+ `[name].[hash].js`+URL_TIMER
		},
		resolve: { // 配置解析别名
			alias: {
				'@': path.resolve(__dirname, 'src'),
				'vue$': 'vue/dist/vue.esm.js'
			}
		},
		devtool: process.env.NODE_ENV === 'production' ? false : 'eval-source-map',
		plugins:process.env.NODE_ENV === 'production'?[
			new CompressionWebpackPlugin({
				algorithm: 'gzip',
				test: /\.(js|css|html)$/,
				threshold: 10240,
				minRatio: 0.8
			})
		]:[]
	},
	css: {
    loaderOptions: {
      // 给 sass-loader 传递选项
      sass: {
        // @/ 是 src/ 的别名
        // 所以这里假设你有 `src/variables.scss` 这个文件
        data: `@import "@/styles/variables.scss";@import "@/styles/mixin.scss";`
      }
    }
	}
}
